﻿using Mono.Data.Sqlite;
using System;
using System.Collections.Generic;
using System.Text;

namespace JyCommonKit.SerializeKit.Sqlite
{
    public class SqliteChannel
    {

        public SqliteChannel(string dbPath)
        {
            _dbPath = $"Data Source={dbPath}";
            OpenDB(_dbPath);
        }


        public void OpenDB(string dbPath)
        {
            try
            {
                _connection = new SqliteConnection(dbPath);

                _connection.Open();

                Console.WriteLine("Connected to db");
            }
            catch (Exception e)
            {
                string temp1 = e.ToString();
                Console.WriteLine(temp1);
            }
        }

        public void CloseSqlConnection()
        {

            if (_command != null) _command.Dispose();
            _command = null;

            if (_reader != null) _reader.Dispose();
            _reader = null;

            if (_connection != null) _connection.Close();
            _connection = null;

            Console.WriteLine("Disconnected from db.");
        }


        public SqliteDataReader ExecuteQuery(string sqlQuery)
        {
            _command = _connection.CreateCommand();
            _command.CommandText = sqlQuery;
            _reader = _command.ExecuteReader();

            return _reader;
        }

        public SqliteDataReader ReadFullTable(string tableName)
        {
            string query = "SELECT * FROM " + tableName;

            return ExecuteQuery(query);
        }

        public SqliteDataReader InsertInto(string tableName, string[] values)
        {
            string query = "INSERT INTO " + tableName + " VALUES (" + values[0];

            for (int i = 1; i < values.Length; ++i)
            {
                query += ", " + values[i];
            }
            query += ")";

            return ExecuteQuery(query);

        }

        public SqliteDataReader UpdateInto(string tableName, string[] cols, string[] colsvalues, string selectkey, string selectvalue)
        {
            string query = "UPDATE " + tableName + " SET " + cols[0] + " = " + colsvalues[0];
            for (int i = 1; i < colsvalues.Length; ++i)
            {
                query += ", " + cols[i] + " =" + colsvalues[i];
            }
            query += " WHERE " + selectkey + " = " + selectvalue + " ";

            return ExecuteQuery(query);
        }

        public SqliteDataReader Delete(string tableName, string[] cols, string[] colsvalues)
        {
            string query = "DELETE FROM " + tableName + " WHERE " + cols[0] + " = " + colsvalues[0];

            for (int i = 1; i < colsvalues.Length; ++i)
            {

                query += " or " + cols[i] + " = " + colsvalues[i];
            }
            Console.WriteLine(query);
            return ExecuteQuery(query);
        }

        public SqliteDataReader InsertIntoSpecific(string tableName, string[] cols, string[] values)
        {
            if (cols.Length != values.Length)
            {
                throw new SqliteException("columns.Length != values.Length");
            }

            string query = "INSERT INTO " + tableName + "(" + cols[0];

            for (int i = 1; i < cols.Length; ++i)
            {
                query += ", " + cols[i];
            }

            query += ") VALUES (" + values[0];

            for (int i = 1; i < values.Length; ++i)
            {
                query += ", " + values[i];
            }

            query += ")";

            return ExecuteQuery(query);
        }

        public SqliteDataReader DeleteContents(string tableName)
        {
            string query = "DELETE FROM " + tableName;

            return ExecuteQuery(query);
        }

        public SqliteDataReader CreateTable(string name, string[] col, string[] colType)
        {
            if (col.Length != colType.Length)
            {
                throw new SqliteException("columns.Length != colType.Length");
            }

            string query = "CREATE TABLE " + name + " (" + col[0] + " " + colType[0];
            for (int i = 1; i < col.Length; ++i)
            {
                query += ", " + col[i] + " " + colType[i];
            }
            query += ")";

            return ExecuteQuery(query);
        }

        public SqliteDataReader SelectWhere(string tableName, string[] items, string[] col, string[] operation, string[] values)
        {
            if (col.Length != operation.Length || operation.Length != values.Length) 
            {
                throw new SqliteException("col.Length != operation.Length != values.Length");
            }

            string query = "SELECT " + items[0];
            for (int i = 1; i < items.Length; ++i)
            {
                query += ", " + items[i];
            }

            query += " FROM " + tableName + " WHERE " + col[0] + operation[0] + "'" + values[0] + "' ";
            for (int i = 1; i < col.Length; ++i)
            {
                query += " AND " + col[i] + operation[i] + "'" + values[0] + "' ";
            }

            return ExecuteQuery(query);
        }

        private string _dbPath;
        private SqliteCommand _command;
        private SqliteDataReader _reader;
        private SqliteConnection _connection;
    }
}
